package com.woniuxy.controller;

import com.woniuxy.entity.CommonResult;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: linKe
 * @Date: 2021/12/28 18:40
 * @FileName: UserController
 * @version: 1.0
 * @Description: 测试注册中心调用数据
 */
@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Value("${server.port}")
    private int port;

    @PostMapping("/create")
    public CommonResult<?> create(@RequestBody User user) {
        userService.create(user);
        return new CommonResult<>("操作成功", 200);
    }

    @GetMapping("/{id}")
    public CommonResult<User> getUser(@PathVariable Long id) throws InterruptedException {
        User user = userService.getUser(id);
        user.setIp(port);
        Thread.sleep(4000);
        log.info("根据id获取用户信息，用户名称为：{}",user.getUsername());
        return new CommonResult<>(user);
    }

    @GetMapping("/getUserByIds")
    public CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids) {
        List<User> userList= userService.getUserByIds(ids);
        log.info("根据ids获取用户信息，用户列表为：{}",userList);
        return new CommonResult<>(userList);
    }

    @GetMapping("/getByUsername")
    public CommonResult<User> getByUsername(@RequestParam String username) {
        User user = userService.getByUsername(username);
        return new CommonResult<>(user);
    }

    @PostMapping("/update")
    public CommonResult<?> update(@RequestBody User user) {
        userService.update(user);
        return new CommonResult<>("操作成功", 200);
    }

    @PostMapping("/delete/{id}")
    public CommonResult<?> delete(@PathVariable Long id) {
        userService.delete(id);
        return new CommonResult<>("操作成功", 200);
    }
}
